package com.gitee.aurora.server.dao.impl;

import com.alibaba.excel.util.DateUtils;
import com.gitee.aurora.core.comm.AuroraJdbcTemplate;
import com.gitee.aurora.core.utils.ResultVO;
import com.gitee.aurora.core.utils.ResultVOUtils;
import com.gitee.aurora.server.dao.PtcjConfigDao;
import com.gitee.aurora.server.vo.PtcjConfig;
import com.gitee.aurora.utils.PageParam;
import com.gitee.aurora.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class PtcjConfigDaoImpl implements PtcjConfigDao {
    @Autowired
    private AuroraJdbcTemplate auroraJdbcTemplate;
    @Override
    public Pagination queryPtjc(PtcjConfig ptcjConfig, PageParam pageParam) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT S_ID AS sId, S_CODE AS sCode, S_CODE_NAME AS sCodeName, PT_CJ AS ptCj, PT_IP AS ptIp, F_CODE AS fCode, F_CODE_NAME AS fCodeName, F_CODE_ORDER AS fCodeOrder, DATE_FORMAT(DT_CREATE_TIME,'%Y-%m-%d %H:%i:%s') AS dtCreateTime,DATE_FORMAT(DT_UPDATE_TIME,'%Y-%m-%d %H:%i:%s') AS dtUpdateTime, S_PATH AS sPath FROM TAB_PTCJ_CONFIG WHERE 1 =1");
        if (ptcjConfig != null){
            if (ptcjConfig.getsCode() != null && !"".equals(ptcjConfig.getsCode())){
                sql.append(" and S_CODE like ?");
                params.add("%"+ptcjConfig.getsCode()+"%");
            }
            if (ptcjConfig.getsCodeName() != null && !"".equals(ptcjConfig.getsCodeName())){
                sql.append(" and S_CODE_NAME like ?");
                params.add("%"+ptcjConfig.getsCodeName()+"%");
            }
            if (ptcjConfig.getPtIp() != null && !"".equals(ptcjConfig.getPtIp())){
                sql.append(" and PT_IP like ?");
                params.add("%"+ptcjConfig.getPtIp()+"%");
            }
            if (ptcjConfig.getfCode() != null && !"".equals(ptcjConfig.getfCode())){
                sql.append(" and F_CODE = ?");
                params.add(ptcjConfig.getfCode());
            }
        }
        return new Pagination(sql.toString(),params.toArray(),pageParam,auroraJdbcTemplate);
    }

    public List<PtcjConfig> query(PtcjConfig ptcjConfig) {
        List<Object> params = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT S_ID AS sId, S_CODE AS sCode, S_CODE_NAME AS sCodeName, PT_CJ AS ptCj, PT_IP AS ptIp, F_CODE AS fCode, F_CODE_NAME AS fCodeName, F_CODE_ORDER AS fCodeOrder, DT_CREATE_TIME AS dtCreateTime, DT_UPDATE_TIME AS dtUpdateTime, S_PATH AS sPath FROM TAB_PTCJ_CONFIG WHERE 1 =1");
        if (ptcjConfig != null){
            if (ptcjConfig.getsCode() != null && !"".equals(ptcjConfig.getsCode())){
                sql.append(" and S_CODE = ?");
                params.add(ptcjConfig.getsCode());
            }

        }
        return auroraJdbcTemplate.query(sql.toString(),new BeanPropertyRowMapper<>(PtcjConfig.class),params.toArray());
    }

    @Override
    public ResultVO updataPtcj(PtcjConfig ptcjConfig) {
        List<Object> params = new ArrayList<>();
        String sql = "UPDATE TAB_PTCJ_CONFIG SET S_CODE_NAME = ?, PT_CJ = ?, PT_IP = ?, F_CODE = ?, F_CODE_NAME = ?, F_CODE_ORDER = ?, DT_UPDATE_TIME = ?, S_PATH = ? WHERE S_CODE = ?";
        if (ptcjConfig == null || ptcjConfig.getsCode() == null || "".equals(ptcjConfig.getsCode()) || ptcjConfig.getPtIp() == null || "".equals(ptcjConfig.getPtIp())){
            return ResultVOUtils.error("请求参数有误");
        }
        params.add(ptcjConfig.getsCodeName());
        params.add(ptcjConfig.getPtCj());
        params.add(ptcjConfig.getPtIp());
        params.add(ptcjConfig.getfCode());
        params.add(ptcjConfig.getfCodeName());
        params.add(ptcjConfig.getfCodeOrder());
        SimpleDateFormat formatter = new SimpleDateFormat(DateUtils.DATE_FORMAT_19);
        params.add(formatter.format(new Date()));
        params.add(ptcjConfig.getsPath());
        params.add(ptcjConfig.getsCode());
        int res = auroraJdbcTemplate.update(sql, params.toArray());
        if (res!=1){
            return ResultVOUtils.error("数据库操作异常");
        }
        return ResultVOUtils.success("更行成功！");
    }

    @Override
    public ResultVO addPtcj(PtcjConfig ptcjConfig) {
        List<Object> params = new ArrayList<>();
        if (ptcjConfig == null || ptcjConfig.getsCode() == null || "".equals(ptcjConfig.getsCode()) || ptcjConfig.getPtIp() == null || "".equals(ptcjConfig.getPtIp())){
            return ResultVOUtils.error("请求参数有误");
        }
        List<PtcjConfig> queryList = query(ptcjConfig);
        if (queryList.size()>0){
            return ResultVOUtils.error("该记录已存在允许再次添加，请进行更新操作");
        }
        String sql = "INSERT INTO TAB_PTCJ_CONFIG ( S_CODE, S_CODE_NAME, PT_CJ, PT_IP, F_CODE, F_CODE_NAME, F_CODE_ORDER, DT_CREATE_TIME, DT_UPDATE_TIME, S_PATH ) VALUES (?,?,?,?,?,?,?,?,?,?)";
        params.add(ptcjConfig.getsCode());
        params.add(ptcjConfig.getsCodeName());
        params.add(ptcjConfig.getPtCj());
        params.add(ptcjConfig.getPtIp());
        params.add(ptcjConfig.getfCode());
        params.add(ptcjConfig.getfCodeName());
        params.add(ptcjConfig.getfCodeOrder());
        SimpleDateFormat formatter = new SimpleDateFormat(DateUtils.DATE_FORMAT_19);
        String format = formatter.format(new Date());
        params.add(format);
        params.add(format);
        params.add(ptcjConfig.getsPath());
        int res = auroraJdbcTemplate.update(sql, params.toArray());
        if (res != 1){
            return ResultVOUtils.error("添加失败，系统异常");
        }
        return ResultVOUtils.success("添加成功");
    }

    @Override
    public ResultVO deletePtcj(List<PtcjConfig> ptcjConfig) {
        String idList = "";
        if (ptcjConfig == null && ptcjConfig.size()==0){
            return ResultVOUtils.error("上传参数有误");
        }
        for (PtcjConfig pt:ptcjConfig) {
            idList=idList+pt.getsId()+",";
        }
        String sql = "DELETE FROM TAB_PTCJ_CONFIG where 1=1 AND S_ID IN ("+idList.replaceFirst(".$", "")+")";
        int res = auroraJdbcTemplate.update(sql);
        if (res != 1){
            return ResultVOUtils.error("数据库操作异常");
        }
        return ResultVOUtils.success("删除成功");
    }
}
